package com.project.web.order.controller.app.AppNative;

import com.project.common.core.utils.exception.Result;
import com.project.common.core.utils.id.IDUtil;
import com.project.web.order.controller.common.BaseController;
import com.project.web.order.model.app.Cart;
import com.project.web.order.service.app.CartService;
import com.project.web.util.exception.ORDER_RESP_CODE_ENUM;
import com.project.web.util.exception.OrderCustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CartComtroller
 * @Description
 * @Author 王天豪
 * @Date 2020/10/22 5:19 PM
 * @Version 1.0
 */

@Api(tags = "【App】购物车API")
@RestController("cartComtroller")
@RequestMapping("/order/auth/appNative/cart")
public class CartComtroller extends BaseController {

    Logger logger = LoggerFactory.getLogger(CartComtroller.class);

    @Resource(name = "cartServiceImpl")
    private CartService cartService;

    @ApiOperation(value = "加入购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "cart", value = "购物车对象", dataType = "Cart", required = true)
    })
    @PostMapping(value = "/addCart")
    public Result addCart(@RequestBody Cart cart) {
        if (cart == null) {
            throw new OrderCustomException(ORDER_RESP_CODE_ENUM.MISS_PARAMETER);
        }

        cart.setId(IDUtil.getId());
        cart.setCreateDate(new Date());
        cartService.save(cart);
        return getResult(null);
    }

    @ApiOperation(value = "查询购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", value = "用户ID", dataType = "String", required = true)
    })
    @GetMapping(value = "/getCartByUserId")
    public Result getCartByUserId(String userId) {
        List<Cart> carts = cartService.selectCartListByUserId(userId);
        return getResult(carts);
    }

    @ApiOperation(value = "删除购物车商品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "购物车ID", dataType = "Long", required = true)
    })
    @GetMapping(value = "/delete")
    public Result delete(Long id) {
        cartService.delete(id);
        return getResult(null);
    }

}
